package action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mgr.DBMgr;
import bean.BoardBean;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class BoardAddAction implements BoardAction {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		String saveDirectory = "D:\\javaJSP\\workspace_ee\\MVCBoard\\WebContent\\saveDirectory";
		String board_name = null;
		String board_pass = null;
		String board_subject = null;
		String board_content = null;
		String board_file = null;
		
		BoardBean bean = new BoardBean();
		DBMgr mgr = new DBMgr();
		
		try {
			MultipartRequest multi = new MultipartRequest(request, saveDirectory,
					1024*1024*10, "utf-8", new DefaultFileRenamePolicy());
			board_name = multi.getParameter("board_name");
			board_pass = multi.getParameter("board_pass");
			board_subject = multi.getParameter("board_subject");
			board_content = multi.getParameter("board_content");
			board_file = multi.getFilesystemName((String) multi.getFileNames().nextElement());
		} catch (IOException e) {
			e.printStackTrace();
		}//trycatch
		bean.setBoard_name(board_name);
		bean.setBoard_pass(board_pass);
		bean.setBoard_subject(board_subject);
		bean.setBoard_content(board_content);
		bean.setBoard_file(board_file);
		
		boolean flag = mgr.boardInsert(bean);
		if(flag){
			try {
				response.sendRedirect("http://localhost/MVCBoard/list.bo");
			} catch (IOException e) {
				e.printStackTrace();
			}//trycatch
		}//if
	}//exe
}//class
